package com.jucai.pppeiyouclient.view.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.jucai.pppeiyouclient.MainActivity;
import com.jucai.pppeiyouclient.R;
import com.jucai.pppeiyouclient.base.BaseActivity;
import com.jucai.pppeiyouclient.base.QuickFragmentAdapter;
import com.jucai.pppeiyouclient.comm.CommonRequestNetData;
import com.jucai.pppeiyouclient.utils.ActivityCollector;
import com.jucai.pppeiyouclient.utils.LogUtils;
import com.jucai.pppeiyouclient.utils.ScreenUtil;
import com.jucai.pppeiyouclient.utils.TextCheckUtils;
import com.jucai.pppeiyouclient.view.fragment.AccompanyRecordFragment;
import com.jucai.pppeiyouclient.view.fragment.ActivityRecordFragment;
import com.jucai.pppeiyouclient.view.fragment.BankFragment;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;

public class TravelRecordActivity extends BaseActivity implements ViewPager.OnPageChangeListener {

    private static final String TAG = "TravelRecordActivity : ";
    private CircleImageView travelImg;
    private TextView travelName, travelInfo;
    private TabLayout travelTab;
    private ViewPager travelVp;
    private String[] titles = new String[]{"个人信息", "陪游记录", "活动记录"};
    private QuickFragmentAdapter<Fragment> fragmentAdapter;
    private List<Fragment> fragments = new ArrayList<>();
    private int position;
    private LinearLayout linearLayout;
    private String sex, name, img, age, phone;

    @Override
    protected void initView(Bundle savedInstanceState) {
        setContentView(R.layout.activity_travel_record);
        ((TextView) findViewById(R.id.common_title_text)).setText("用户出游记录");

        fragments.add(new BankFragment());
        fragments.add(new AccompanyRecordFragment());
        fragments.add(new ActivityRecordFragment());

        fragmentAdapter = new QuickFragmentAdapter<>(getSupportFragmentManager(), fragments, titles);
        travelImg = (CircleImageView) findViewById(R.id.travel_img);
        travelName = (TextView) findViewById(R.id.travel_name);
        travelInfo = (TextView) findViewById(R.id.travel_info);
        travelTab = (TabLayout) findViewById(R.id.travel_tab);
        travelVp = (ViewPager) findViewById(R.id.travel_vp);
        travelVp.setAdapter(fragmentAdapter);
        travelTab.setupWithViewPager(travelVp);

        position = getIntent().getIntExtra("position", 1);

        if (position == 2) {
            travelVp.setCurrentItem(2);
        } else {
            travelVp.setCurrentItem(1);
        }
        LogUtils.e("position = " + position + ",current = " + travelVp.getCurrentItem());

        linearLayout = (LinearLayout) travelTab.getChildAt(0);
        linearLayout.setShowDividers(LinearLayout.SHOW_DIVIDER_MIDDLE);
        linearLayout.setDividerDrawable(ContextCompat.getDrawable(TravelRecordActivity.this, R.drawable.shape_vertical_divider_gray));
        linearLayout.setDividerPadding(ScreenUtil.dip2px(TravelRecordActivity.this, 15));
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void loadNetData() {

        HashMap<String, String> param = new HashMap<>();
        param.put("service", "Conductor_Person.getlist");
        param.put("id", getIntent().getStringExtra("user_id"));
        LogUtils.e(TAG + "param = " + param.toString());

        CommonRequestNetData.getJsonObjectData(TravelRecordActivity.this, param, true, new CommonRequestNetData.JsonObjectCallBack() {
            @Override
            public void parseJsonObject(JSONObject jsonObject) {
                LogUtils.e(TAG + "jsObj Travel = " + jsonObject.toString());
                try {

                    jsonObject = jsonObject.getJSONObject("userinfo");
                    img = jsonObject.getString("member_avatar");
                    name = jsonObject.getString("member_realname");
                    sex = jsonObject.getString("member_sexual");
                    age = jsonObject.getString("member_age");
                    phone = jsonObject.getString("member_phone");

                    if (TextUtils.equals(sex,"1")) {
                        sex = "男";
                    } else {
                        sex = "女";
                    }

                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            if (TextCheckUtils.isEmpty(img)) {
                                travelImg.setImageResource(R.drawable.avatar);
                            } else {
                                Glide.with(TravelRecordActivity.this)
                                        .load(img)
                                        .centerCrop()
                                        .override(80, 80)
                                        .into(travelImg);
                            }
                            travelName.setText(name);
                            travelInfo.setText(sex + "  " + age);
                        }
                    });

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public static void startTravelRecord(Context context, int position, String userId) {
        Intent intent = new Intent(context, TravelRecordActivity.class);
        intent.putExtra("position", position);
        intent.putExtra("user_id", userId);
        context.startActivity(intent);
    }

    @Override
    protected void setListener() {
        findViewById(R.id.common_back).setOnClickListener(this);
        findViewById(R.id.common_home_back).setOnClickListener(this);
        travelVp.addOnPageChangeListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.common_back:
                ActivityCollector.removeActivity(this);
                break;

            case R.id.common_home_back:
                startActivity(new Intent(TravelRecordActivity.this, MainActivity.class));
                ActivityCollector.removeActivity(this);
                break;
        }
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        if (position == 0) {
            ActivityCollector.removeActivity(this);
        }
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }
}
